package com.becca.cake.service.system.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.becca.cake.dao.system.AdminDAO;
import com.becca.cake.model.system.AdminDO;
import com.becca.cake.service.system.IUserLoginService;
import com.becca.cake.util.MD5Util;

/**
 * 员工管理Service实现 
 * @author hejinguo
 * @version $Id: UserLoginServiceImpl.java, v 0.1 2013-12-12 下午05:34:37
 */
@Service
public class UserLoginServiceImpl implements IUserLoginService {
    @Autowired
    private AdminDAO priAdminDAO;

    @Override
    public AdminDO login(String name, String passWord) throws Exception {
        AdminDO priAdminDO = this.priAdminDAO.selectAdminByName(name);
        //员工存在并且密码相同
        if (priAdminDO != null && MD5Util.getMD5Password(passWord).equals(priAdminDO.getPass())) {
            return priAdminDO;
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updatePassWord(AdminDO priAdminDO, String oldPass, String newPass) throws Exception {
        if (MD5Util.getMD5Password(oldPass).equals(priAdminDO.getPass())) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", priAdminDO.getId());
            map.put("password", MD5Util.getMD5Password(newPass));
            this.priAdminDAO.updatePassWord(map);
        } else {
            throw new Exception("旧密码错误!");
        }
    }
}
